//
//  PPIVideoViewController.m
//  tangshantongjiAPP
//
//  Created by yuntai on 2018/4/3.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "PPIVideoViewController.h"
#import "CommonModel.h"
#import "NSString+Time.h"
#import "UIView+Frame.h"
#import "MRVLCPlayer.h"

@interface PPIVideoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) MRVLCPlayer *player;
@end

@implementation PPIVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"PPI专业培训视频";
    self.array = [NSMutableArray array];
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight)];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 100;
    tableview.tableFooterView = [[UIView alloc]init];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];

    
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.player dismiss];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellid = @"ppivideocellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        UIImageView *iv = [[UIImageView alloc]init];
        iv.frame = CGRectMake(10, 10, 120, 80);
        iv.tag = 100;
        [cell.contentView addSubview:iv];
        
        UILabel *namel = [[UILabel alloc]initWithFrame:CGRectMake(iv.x+iv.width+10., iv.y, kScreenBoundWidth-200, 30)];
        namel.tag = 101;
        namel.textColor = kTextDeepDarkColor;
        namel.font = [UIFont systemFontOfSize:14];
        [cell.contentView addSubview:namel];
        
        UILabel *namel2 = [[UILabel alloc]initWithFrame:CGRectMake(iv.x+iv.width+10., iv.y+iv.height-30, kScreenBoundWidth-200, 30)];
        namel2.tag = 102;
        namel2.textColor = kTextDeepDarkColor;
        namel2.font = [UIFont systemFontOfSize:14];
        [cell.contentView addSubview:namel2];
    }
    CommonModel *model = self.array[indexPath.row];
    
    UILabel *nl = [cell.contentView viewWithTag:101];
    nl.text = model.title?:@"";
    
    UILabel *snl = [cell.contentView viewWithTag:102];
    snl.text = [NSString getStringWithNsTimerIntervall:[model.video_length doubleValue]/1000];
    
    UIImageView *iv = [cell.contentView viewWithTag:100];
    [iv sd_setImageWithURL:[NSURL URLWithString:model.fetch_frame?:@""] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonModel *model = self.array[indexPath.row];
    self.player = [[MRVLCPlayer alloc] init];
    
    self.player.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width / 16 * 9);
    self.player.center = self.view.center;
    self.player.mediaURL = [NSURL URLWithString:model.video_path];
    [self.player showInView:self.view.window];
}


- (void)getdata{
    
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    [parames setValue:@(self.currentPage) forKey:@"page"];
    [parames setValue:kTokenID forKey:@"token"];
    
    [NetWork PostNetWorkWithUrl:@"api/ppi/getList.ajax" with:parames successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];

        NSArray *temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];

        [HUDManager showWarningWithText:msg];
    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];

    }];

    
}
@end
